#include "RocketGun.h"
#include "ObjectManager.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_XAudio2.h"
#include "Player.h"
#include "CreateProjectileMessage.h"
#include "MessageSystem.h"
#include "GamePlayState.h"

RocketGun::RocketGun(Player* owner)
{
	SetWidth(16);
	SetHeight(5);
	SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/RocketLauncher.png")));

	SetMetalPrice(100);
	SetCircuitPrice(50);

	player = owner;
	//player->AddRef();
	SetX(owner->GetX() + owner->GetWidth());
	SetY(owner->GetY());

	ObjectManager::GetInstance()->AddObject(this);
	Release();

	type = OBJ_WEAPON;
	rangedType = RW_ROCKET;
	attackReady = false;
	specialReady = false;
	ROF = 2.0f;
	shotTimer = ROF;
	cooldownSeconds = 15.0f;
	cooldownTimer = cooldownSeconds;
	energyCost = 30;
	priceScrap = 100;
	priceCircuit = 50;

	//rocketShot = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Ranged Weapons/rocketShot.wav"));

	emitter = nullptr;
}

RocketGun::~RocketGun()
{
	if(GetTextureID() != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(GetTextureID());
		SetTextureID(-1);
	}
	//if(rocketShot != -1)
	//{
	//	CSGD_XAudio2::GetInstance()->SFXStopSound(rocketShot);
	//	CSGD_XAudio2::GetInstance()->SFXUnloadSound(rocketShot);
	//	rocketShot = -1;
	//}

	ObjectManager::GetInstance()->RemoveObject(this);
}

bool RocketGun::CheckCollision(IEntity* base)
{
	return false;
}

void RocketGun::Attack()
{
	if(GetOwner()->GetClock() == false)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(ROCKET, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		//CSGD_XAudio2::GetInstance()->SFXPlaySound(rocketShot);
	}
	else if(GetOwner()->GetClock() == true)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(ROCKET_CLOCKED, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		//CSGD_XAudio2::GetInstance()->SFXPlaySound(rocketShot);
	}

	attackReady = false;
	shotTimer = 0;
}

void RocketGun::SpecialAttack() 
{
	if(GetOwner()->GetClock() == false)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(MISSLE_BARRAGE, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
	}
	else if(GetOwner()->GetClock() == true)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(MISSLE_BARRAGE, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
	}

	specialReady = false;
	cooldownTimer = 0;
}

void RocketGun::Render()
{
	SetDirection(player->GetDirection());

	SetX(player->GetX() + player->GetWidth());
	SetY(player->GetY());

	RECT image = {};

	switch(GetDirection())
	{
	case 3:
		{
			image.top = 7;
			image.bottom = 13;
			image.left = 0;
			image.right = 5;
			SetWidth(5);
			SetHeight(6);
			SetX(player->GetX() + 50);
			SetY(player->GetY() + 30);
			
		}
		break;

	case 0:
		{
			image.top = 0;
			image.bottom = 5;
			image.left = 0;
			image.right = 16;
			SetWidth(16);
			SetHeight(5);
			SetX(player->GetX() + 25);
			SetY(player->GetY() + 30);
		}
		break;

	case 2:
		{
			image.top = 15;
			image.bottom = 21;
			image.left = 0;
			image.right = 5;
			SetWidth(5);
			SetHeight(6);
			SetX(player->GetX() + 20);
			SetY(player->GetY() + 35);
		}
		break;
	}


	if(player->GetCurWeapon() == 2)
	{
		if(GetTextureID() != -1)
		{
			CSGD_TextureManager::GetInstance()->Draw(GetTextureID(),
			int(GetX() - GamePlayState::GetInstance()->GetCamX() - player->GetWidth()/2),
			int(GetY() - GamePlayState::GetInstance()->GetCamY()), 2.0f, 2.0f ,&image);
		}
	}


}
